<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//社区分类管理
class Bbs_article_model extends CI_Model{

        function __construct()
        {
                parent::__construct();
                $this->table = 'bbs_article';//文章分类
                $this->bbs_article_category = 'bbs_article_category';
                $this->users_info = 'users_info';
                $this->bbs_user = 'bbs_user';
                $this->bbs_article_comment = 'bbs_article_comment';
                $this->bbs_article_tags = 'bbs_article_tags';
                $this->bbs_wenda_tags = 'bbs_wenda_tags';
                $this->bbs_exposure = 'bbs_exposure';
                $this->bbs_exposure_tags = 'bbs_exposure_tags';
        }
        function getCount($where,$like){
            $this->db->select('*');
            $this->db->from($this->table);
            $this->db->join($this->bbs_article_category, $this->table.'.cat_id = '.$this->bbs_article_category.'.id', 'left');
            $this->db->join($this->bbs_article_category." c",c.'.id='.$this->bbs_article_category.'.pid','left');
            $this->db->where($where);
            if($like){
                    $this->db->like($this->table.'.title',$like);
            }
            $query = $this->db->get();
            return $query->num_rows();
        }
        function getList($where,$page,$limit,$like){
            $this->db->select($this->table.'.*,'.$this->bbs_article_category.'.name,'.c.'.name cname,'.$this->users_info.'.nickname,'.$this->bbs_user.".group_id");
            $this->db->from($this->table);
            $this->db->join($this->bbs_article_category, $this->table.'.cat_id = '.$this->bbs_article_category.'.id', 'left');
            $this->db->join($this->users_info,$this->table.'.authorid='.$this->users_info.'.user_id','left');
            $this->db->join($this->bbs_article_category." c",c.'.id='.$this->bbs_article_category.'.pid','left');
            $this->db->join($this->bbs_user,$this->table.'.authorid = '.$this->bbs_user.'.user_id','left');
            $this->db->where($where);
            if($like){
                    $this->db->like($this->table.'.title',$like);
            }
            $this->db->order_by("id", "desc");
            $this->db->limit($limit, $page);
            $query = $this->db->get();
            return $query->result_array(); 
        }
                //设置状态
        function setstatus($status,$id)
        {
                return $this->db->where_in('id',$id)->update($this->table, array('status'=>$status,'check_time'=>time()));
        }
        function status($status,$id,$type)
        {
               return $this->db->where_in('id',$id)->update($this->table,array($type=>$status));
        }
        function delete($id)
        {
             return $this->db->where_in('id',$id)->update($this->table,array('show'=>0));
        }
        function Get($id)
        {
                return $this->db->select('*')
                  ->from($this->table)
                  ->where('id' , $id)
                  ->get()
                  ->row_array();
        }
        function Getinformation($id)
        {
            return $this->db->select('*')
                ->from($this->bbs_exposure)
                ->where('id' , $id)
                ->get()
                ->row_array();
        }
        //更新数据
        function update($data,$id)
        {
            return $this->db->where('id',$id)->update($this->table, $data);
        }
        function information_edit($data,$id)
        {
            return $this->db->where('id',$id)->update($this->bbs_exposure, $data);
        }
        //插入数据
        function insert($data)
        {
                $this->db->insert($this->table, $data);
                return $this->db->affected_rows();
        }
        function information_add($data)
        {
            $this->db->insert($this->bbs_exposure, $data);
            return $this->db->affected_rows();
        }
        function comment_count($where,$like){
            $this->db->select('*');
            $this->db->from($this->bbs_article_comment);
            $this->db->where($where);
            if($like){
                    $this->db->like($this->bbs_article_comment.'.title',$like);
            }
            $query = $this->db->get();
            return $query->num_rows();
        }
        function commentList($where,$page,$limit,$like){
            $this->db->select('*');
            $this->db->from($this->bbs_article_comment);
            $this->db->where($where);
            if($like){
                    $this->db->like($this->bbs_article_comment.'.title',$like);
            }
            $this->db->order_by("id", "desc");
            $this->db->limit($limit, $page);
            $query = $this->db->get();
            return $query->result_array(); 
        }
        function commshow($data,$id){
            return $this->db->where('id',$id)->update($this->bbs_article_comment,$data);
        }
        //文章标签统计
        function getarttagsCount($where,$like){
            $this->db->select('*');
            $this->db->from($this->bbs_article_tags);
            $this->db->where($where);
            if($like){
                    $this->db->like($this->bbs_article_tags.'.name',$like);
            }
            $query = $this->db->get();
            return $query->num_rows();
        }
        //文章标签列表
        function getarttagsList($where,$page,$limit,$like){
             $this->db->select("*");
            $this->db->from($this->bbs_article_tags);
            $this->db->where($where);
            if($like){
                    $this->db->like($this->bbs_article_tags.'.name',$like);
            }
            $this->db->order_by("sort", "desc");
            $this->db->limit($limit, $page);
            $query = $this->db->get();
            return $query->result_array(); 
        }
        //设置标签状态
        function setarttag($status,$id,$type){
            return $this->db->where_in('id',$id)->update($this->bbs_article_tags,array($type=>$status));
        }
        
        function setwdtag($status,$id,$type){
            return $this->db->where_in('id',$id)->update($this->bbs_wenda_tags,array($type=>$status));
        }
        function setexptag($status,$id,$type){
            return $this->db->where_in('id',$id)->update($this->bbs_exposure_tags,array($type=>$status));
        }
        //添加标签
        function insertarttag($data)
        {
                $this->db->insert($this->bbs_article_tags, $data);
                return $this->db->affected_rows();
        }
        function insertwdtag($data)
        {
                $this->db->insert($this->bbs_wenda_tags, $data);
                return $this->db->affected_rows();
        }
        function insertexptag($data)
        {
            $this->db->insert($this->bbs_exposure_tags, $data);
            return $this->db->affected_rows();
        }
        //修改文章标签
        function updatearttag($data,$id){
             return $this->db->where('id',$id)->update($this->bbs_article_tags, $data);
        }
        function Getarttag($id){
            return $this->db->select('*')
                  ->from($this->bbs_article_tags)
                  ->where('id' , $id)
                  ->get()
                  ->row_array();
        }
        //问答标签统计
        function getwdtagsCount($where,$like){
            $this->db->select('*');
            $this->db->from($this->bbs_wenda_tags);
            $this->db->where($where);
            if($like){
                    $this->db->like($this->bbs_wenda_tags.'.name',$like);
            }
            $query = $this->db->get();
            return $query->num_rows();
        }
        function getexptagsCount($where,$like){
            $this->db->select('*');
            $this->db->from($this->bbs_exposure_tags);
            $this->db->where($where);
            if($like){
                $this->db->like($this->bbs_exposure_tags.'.name',$like);
            }
            $query = $this->db->get();
            return $query->num_rows();
        }
        //问答标签列表
        function getwdtagsList($where,$page,$limit,$like){
             $this->db->select("*");
            $this->db->from($this->bbs_wenda_tags);
            $this->db->where($where);
            if($like){
                    $this->db->like($this->bbs_wenda_tags.'.name',$like);
            }
            $this->db->order_by("sort", "desc");
            $this->db->limit($limit, $page);
            $query = $this->db->get();
            return $query->result_array(); 
        }
        function getexptagsList($where,$page,$limit,$like){
            $this->db->select("*");
            $this->db->from($this->bbs_exposure_tags);
            $this->db->where($where);
            if($like){
                $this->db->like($this->bbs_exposure_tags.'.name',$like);
            }
            $this->db->order_by("sort", "desc");
            $this->db->limit($limit, $page);
            $query = $this->db->get();
            return $query->result_array();
        }
        //修改问答标签
        function updatewdtag($data,$id){
             return $this->db->where('id',$id)->update($this->bbs_wenda_tags, $data);
        }
        function updateexptag($data,$id){
            return $this->db->where('id',$id)->update($this->bbs_exposure_tags, $data);
        }
        function Getwdtag($id){
            return $this->db->select('*')
                  ->from($this->bbs_wenda_tags)
                  ->where('id' , $id)
                  ->get()
                  ->row_array();
        }
        function Getexptag($id){
            return $this->db->select('*')
                ->from($this->bbs_exposure_tags)
                ->where('id' , $id)
                ->get()
                ->row_array();
        }
}